#ifndef __TCPCONNECTION_H_
#define __TCPCONNECTION_H_
#include"EventLoop.h"
#include"Address.h"
#include"SocketIO.h"
#include"Noncopyable.h"
#include"Socket.h"
#include"../../libs/Mylog.h"
#include<functional>
#include<memory>
#include<string>

using std::string;
using std::shared_ptr;


namespace SC
{
    class EventLoop;
    class TcpConnection;
    using TcpConnectionPtr = shared_ptr<TcpConnection>;
    using TcpConnectionCallBack = std::function<void(const TcpConnectionPtr&)>;

    class TcpConnection
    : Noncopyable, public std::enable_shared_from_this<TcpConnection>
    {
    public:
        TcpConnection(int, EventLoop*);
        ~TcpConnection();

        string recv();
        void send(const string& msg);
        void sendInLoop(const string& msg);

        string address() const;
        void shutdown();
        int fd() const;

        void setConnectCB(const TcpConnectionCallBack&);
        void setMessageCB(const TcpConnectionCallBack&);
        void setCloseCB(const TcpConnectionCallBack&);

        void handleConnectCB();
        void handleMessageCB();
        void handleCloseCB();

    private:
        Address getLocal();
        Address getPeer();

    private:
        Socket _sock;
        SocketIO _sio;
        bool _isShutdownWrite;

        Address _local;
        Address _peer; 

        EventLoop* _loop;
        TcpConnectionCallBack _cb_connection;
        TcpConnectionCallBack _cb_msg;
        TcpConnectionCallBack _cb_close;
    };
    
}
#endif